//
//  MyCollectShopServiceTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2019/1/21.
//  Copyright © 2019年 Michael Li. All rights reserved.
//

#import "MyCollectShopServiceTabVC.h"
#import "ServiceModel.h"
#import "ShopSubModel.h"
#import "WriteRequirementTitleCell.h"
#import "ServiceCell.h"
@interface MyCollectShopServiceTabVC ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) NSDictionary *dict;

@property(nonatomic,strong) MCButton *preBtn;
@property(nonatomic,assign) NSInteger btnTag;
@property(nonatomic,strong) UIButton *sureBtn;
//@property(nonatomic,strong) MyShopHeadView *headView;
@property(nonatomic,strong) UIView *shoeView;
@property(nonatomic,strong) MJRefreshAutoNormalFooter *footer;

@end
static NSString *identifierShop = @"shopCell";
static NSString *identifierShopOnOFF = @"shopOnOffCell";
static NSString *identifierWriteRequirementTitleCell = @"WriteRequirementTitleCell";
static NSString *identifierEvaluation = @"evaluationCell";
static NSString *identifierService = @"ServiceCell";
static NSString *identifierNeed = @"DemandCell";
@implementation MyCollectShopServiceTabVC
-(NSMutableArray *)serviceArray
{
    if (!_serviceArray) {
        _serviceArray = [NSMutableArray array];
        
    }
    return _serviceArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
    self.tableView.mj_footer = self.footer;
    [self.footer setTitle:[NSBundle bundleLocalizedStringForKey:@"footerTitle"]
                 forState:MJRefreshStateNoMoreData];
    self.footer.stateLabel.font = [UIFont systemFontOfSize:12];
    self.footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    [self.tableView registerNib:[UINib nibWithNibName:@"ShopCell" bundle:nil] forCellReuseIdentifier:identifierShop];
    [self.tableView registerNib:[UINib nibWithNibName:@"WriteRequirementTitleCell" bundle:nil] forCellReuseIdentifier:identifierWriteRequirementTitleCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"ShopOnlineOfflineCell" bundle:nil] forCellReuseIdentifier:identifierShopOnOFF];
    [self.tableView registerNib:[UINib nibWithNibName:@"DemandCell" bundle:nil] forCellReuseIdentifier:identifierNeed];
    [self.tableView registerNib:[UINib nibWithNibName:@"ServiceCell" bundle:nil] forCellReuseIdentifier:identifierService];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(associationSearch:) name:@"associationSearch" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusibai:) name:@"qingqiusibai" object:nil];
    [self.tableView registerNib:[UINib nibWithNibName:@"EvaluationCell" bundle:nil] forCellReuseIdentifier:identifierEvaluation];
    self.page = 1;
    self.row = 10;
    self.btnTag = 1;
    [self reloadInternetData];
}
-(void)reloadInternetData {
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    self.dict = @{
                  @"uid":[NumberSwitchString numberSwitchString:self.model.uid],
                  @"associationtype":@"",
                  @"associationtypesub1":@"",
                  @"associationtypesub2":@"",
                  @"commonindustry":@"",
                  @"industry":@"",
                  @"type":@"",
                  @"address":@"",
                  @"order":@"",
                  @"page":[NumberSwitchString numberSwitchString:self.page],
                  @"rows":[NumberSwitchString numberSwitchString:self.row]
                  };
    XLog(@"服务self.dict:%@",self.dict);
    [CustomeManager POSTWithURLIndex:24 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
-(void)reloadMore
{
    self.page ++;
    [self reloadInternetData];
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    
}
#pragma mark-代理:查询服务
-(void)associationSearch:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSArray *date = [dict objectForKey:@"date"];
    if (date.count) {
        for (NSDictionary *dict in date) {
            ServiceModel *model = [ServiceModel mj_objectWithKeyValues:dict];
            [self.serviceArray addObject:model];
        }
        [self.tableView.mj_header endRefreshing];
    }
    if (self.serviceArray.count) {
        self.footer.hidden = NO;
        if (date.count < 10) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }else {
            [self.tableView.mj_footer endRefreshing];
        }
    }else {
        self.footer.hidden = YES;
        //        [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
        
        [self.tableView.mj_header endRefreshing];
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
    }
    [self.tableView reloadData];
    [WSProgressHUD dismiss];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.serviceArray.count?self.serviceArray.count+1:0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    if (indexPath.row == 0) {
        WriteRequirementTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierWriteRequirementTitleCell forIndexPath:indexPath];
        cell.titleL.text = @"在销售服务";
        return cell;
    }else  {
        ServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierService forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.serviceArray.count) {
            ServiceModel *model = self.serviceArray[indexPath.row-1];
            cell.serviceModel = model;
        }
        return cell;
        
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 45;
    }else {
        return 124;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     ServiceModel *model = self.serviceArray[indexPath.row-1];
    if (self.returnValueBlock) {
        self.returnValueBlock(model);
    };
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"empty"];
    return image;
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = [NSBundle bundleLocalizedStringForKey:@"noData"];
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:kLabel102Color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
@end
